<template>
	<a-modal :title="model=='1'?'新增帮扶':'编辑帮扶'" :width="640" :visible="visible" :confirmLoading="loading"
		@ok="handleOk" @cancel="handleCancel">
		<a-spin :spinning="loading">
			<a-form layout='horizontal'>
				<a-form-item label="选择帮扶门店" :wrapperCol='wrapperCol' :labelCol="labelCol">
					<a-select v-model="cardInfo.from_store_id" :allowClear='true'  placeholder="请选择门店">
					  <a-select-option value="" disabled>请选择门店</a-select-option>
					  <a-select-option :value="item.id" v-for="(item,index) in storeList" :key='item.id'>{{item.name}}</a-select-option>
					</a-select>
				</a-form-item>
				<a-form-item label="帮扶门店接单数" :wrapperCol='wrapperCol' :labelCol="labelCol">
					<a-input placeholder='接单数' v-model="cardInfo.from_store_nums" :allowClear='true' type='number'/>
				</a-form-item>
				<a-form-item label="选择被帮扶门店" :wrapperCol='wrapperCol' :labelCol="labelCol">
					<a-select v-model="cardInfo.to_store_id" :allowClear='true'  placeholder="请选择门店">
					  <a-select-option value="" disabled>请选择门店</a-select-option>
					  <a-select-option :value="item.id" v-for="(item,index) in storeList" :key='item.id'>{{item.name}}</a-select-option>
					</a-select>
				</a-form-item>
				<a-form-item label="有效期" :wrapperCol='wrapperCol' :labelCol="labelCol">
					<a-range-picker v-model='cardInfo.range' class="medio-width" valueFormat='YYYY/MM/DD' format="YYYY/MM/DD" :showTime='false' allowClear @change='exchangeTime'/>
				</a-form-item>
			</a-form>
		</a-spin>
	</a-modal>
</template>

<script>
	import { helpAdd,helpEdit} from '@/api/assist';
	import { storeList } from '@/api/electronCard'
	export default {
		props: {
			model: {
				type: String,
				default: '1',
			},
			visible: {
				type: Boolean,
				default: false,
			},
			store:{
				type: Array,
				default: [],
			},
			cardInfo:{
				default: {
					id:'',
					name: '',
					from_store_id:'',
					to_store_id:'',
					from_store_nums:'',
					start_time:'',
					end_time:'',
					range: [],
				},
			}
		},
		data() {
			return {
				loading: false,
				labelCol: {
					style: {
						width: '120px',
					},
				},
				wrapperCol:{
					style: {
						width: '350px',
						display:'inline-block',
					},
				},
			}
		},
		created(){
			
		},
		computed:{
			storeList:{
				get() {
					return this.store;
				},
				set(newValue) {
					this.$emit('update:value', newValue);
				}
			},
		},
		methods: {
			// 有效期处理
			exchangeTime(e){
				this.cardInfo.start_time = this.cardInfo.range[0];
				this.cardInfo.end_time = this.cardInfo.range[1];
			},
			// 提交设置
			handleOk() {
				if(!this.cardInfo.from_store_id){
					this.$message.error('请选择门店');
					return
				}
				if(!this.cardInfo.from_store_nums){
					this.$message.error('接单数不能为空');
					return
				}
				if(!this.cardInfo.to_store_id){
					this.$message.error('请选择被帮扶门店');
					return
				}
				if(this.cardInfo.range.length != '2'){
					this.$message.error('有效期不能为空');
					return
				}
				this.loading = true;
				if(this.model == '1'){
					helpAdd(this.cardInfo).then(res=>{
						this.loading = false;
						if(res.status == '0'){
							this.$emit('closeModal');
						}else{
							this.$message.error(res.msg);
						}
					})
				}else{
					helpEdit(this.cardInfo).then(res=>{
						this.loading = false;
						if(res.status == '0'){
							this.$emit('closeModal');
						}else{
							this.$message.error(res.msg);
						}
					})
				}
			},
			handleCancel() {
				this.$emit('closeModal');
			},
		}
	}
</script>